﻿using System.IO;
using DDU.Infrastructure;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;
using System.IO.IsolatedStorage;
using System.Text;
using System.Xml.Linq;

namespace DDU.DataModel
{
    public class FictionInfo : NotificationObject
    {
        public FictionInfo() { }

        public FictionInfo(string fileName)
        {
            FileName = fileName;

            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream stream = store.OpenFile(fileName, FileMode.Open, FileAccess.ReadWrite);
                string readcontent = new StreamReader(stream).ReadToEnd();
                byte[] buffer = Convert.FromBase64String(readcontent);
                string xmlContent = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                XDocument xdocument = XDocument.Parse(xmlContent, LoadOptions.None);

                XElement root = xdocument.Element("fiction");
                Title = root.Attribute("title").Value;
                Author = root.Attribute("author").Value;

                Summary = root.Element("summary").Value;
                Content = root.Element("content").Value;

                string coverString = root.Element("cover").Value;
                byte[] coverBuffer = Convert.FromBase64String(coverString);
                Cover = new MemoryStream(coverBuffer);
                stream.Close();
            }
        }

        #region Fields
        private string _title;
        private string _author;
        private Stream _cover;
        private string _summary;
        private string _content;
        private Int64 _pageSize = 2000;
        private Int64 _readLength = 0;
        private string _fileName;
        #endregion

        #region Properties
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public string Author
        {
            get { return _author; }
            set
            {
                _author = value;
                RaisePropertyChanged(() => Author);
            }
        }

        public Stream Cover
        {
            get { return _cover; }
            set
            {
                _cover = value;
                RaisePropertyChanged(() => Cover);
            }
        }

        public string Summary
        {
            get { return _summary; }
            set
            {
                _summary = value;
                RaisePropertyChanged(() => Summary);
            }
        }

        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                RaisePropertyChanged(() => Content);
                RaisePropertyChanged(() => PageCollection);
                RaisePropertyChanged(() => TotalPages);
            }
        }

        public string FileName
        {
            get { return _fileName; }
            set
            {
                _fileName = value;
                RaisePropertyChanged(() => FileName);
            }
        }

        public Int64 PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                RaisePropertyChanged(() => PageSize);
                RaisePropertyChanged(() => PageCollection);
                RaisePropertyChanged(() => TotalPages);
            }
        }

        public Int64 TotalPages
        {
            
            get
            {
                return (Content.Length + PageSize - 1) / PageSize;
            }
        }

        public Int64 ReadLength
        {
            get { return _readLength; }
            set
            {
                _readLength = value;
                RaisePropertyChanged(() => ReadLength);
                //CurrentIndex = ((ReadLength + PageSize - 1) / PageSize) + 1;
            }
        }

        public IEnumerable<FictionItemInfo> PageCollection
        {
            get
            {
                for (Int64 i = 0; i < TotalPages; i++)
                {
                    if (i == TotalPages - 1)
                    {
                        yield return new FictionItemInfo(i, Content.Substring(Convert.ToInt32((i * PageSize))));
                    }
                    else
                    {
                        yield return new FictionItemInfo(i, Content.Substring(Convert.ToInt32(i * PageSize), Convert.ToInt32(PageSize)));
                    }
                }
            }
        }
        #endregion
    }
}
